<%@page import="com.zhuoshi.dao.ContractSubDao"%>
<%@page import="com.zhuoshi.bean.ContractSubBean"%>
<%@page import="com.zhuoshi.utils.DateHelper"%>
<%@page import="com.zhuoshi.bean.ContractMasterBean"%>
<%@page import="com.zhuoshi.dao.ContractMasterDao"%>
<%@page import="com.zhuoshi.dao.PurchaseDao"%>
<%@page import="com.zhuoshi.bean.PurchaseBean"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
request.setCharacterEncoding("UTF-8");
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

//合同数字编号
ContractMasterDao dao = new ContractMasterDao();
List<ContractMasterBean> list = dao.getAllList();
pageContext.setAttribute("list", list);

//绑定页面基本数据
String id = request.getParameter("id");
PurchaseDao purchDao = new PurchaseDao();
PurchaseBean purchBean = purchDao.getModel(Integer.parseInt(id));
pageContext.setAttribute("purchBean", purchBean);

//备件名称
ContractSubDao csDao = new ContractSubDao();
ContractSubBean csBean = new ContractSubBean();
csBean.setNumerical(purchBean.getNumerical());
List<ContractSubBean> csList = csDao.getList(csBean) ;
pageContext.setAttribute("csList", csList);



//处理数据
String action = request.getParameter("action");
if("edit".equals(action)){
	String numerical = request.getParameter("numerical");
	String sqarename = request.getParameter("sqare");
	String modeltype = request.getParameter("model");
	String unit = request.getParameter("prickle");
	String totalcount = request.getParameter("amount");
	String price = request.getParameter("price");
	String totalmoney = request.getParameter("totalmoney");
	String supplier = request.getParameter("supplier");
	String purchasedate = request.getParameter("purdate");
	
	PurchaseDao purDao = new PurchaseDao();
	PurchaseBean purBean = new PurchaseBean();
	purBean.setNumerical(Integer.parseInt(numerical));
	purBean.setSqarename(sqarename);
	purBean.setModeltype(modeltype);
	purBean.setUnit(unit);
	purBean.setTotalcount(Integer.parseInt(totalcount));
	purBean.setPrice(Double.parseDouble(price));
	purBean.setTotalmoney(Double.parseDouble(totalmoney));
	purBean.setSupplier(supplier);
	purBean.setPurchasedate(DateHelper.strToDate(purchasedate));
	purBean.setId(Integer.parseInt(id));
	int rows = purDao.update(purBean);
	if(rows>0){
		out.print("<script language='javascript'>alert('保存成功');location.href='purchase_update.jsp?id="+purchBean.getId()+"';</script>");
	}else{
		out.print("<script language='javascript'>alert('保存失败');</script>");
	}
	//out.print(numerical+" | "+sqarename+" | "+modeltype+" | "+unit+" | "+totalcount+" | "+price+" | "+totalmoney+" | "+supplier+" | "+purchasedate);
}

%>

<!-- saved from url=(0043)http://localhost:8080/EMIS/purchase_add.jsp -->
<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta charset="utf-8">
<meta name="renderer" content="webkit|ie-comp|ie-stand">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no">
<meta http-equiv="Cache-Control" content="no-siteapp">
<link rel="Bookmark" href="http://localhost:8080/favicon.ico">
<link rel="Shortcut Icon" href="http://localhost:8080/favicon.ico">
<!--[if lt IE 9]>
<script type="text/javascript" src="lib/html5.js"></script>
<script type="text/javascript" src="lib/respond.min.js"></script>
<script type="text/javascript" src="lib/PIE_IE678.js"></script>
<![endif]-->
<link rel="stylesheet" type="text/css" href="template/H-ui.min.css">
<link rel="stylesheet" type="text/css" href="template/H-ui.admin.css">
<link rel="stylesheet" type="text/css" href="template/iconfont.css">
<link rel="stylesheet" type="text/css" href="template/icheck.css">
<link rel="stylesheet" type="text/css" href="template/skin.css" id="skin">
<link rel="stylesheet" type="text/css" href="template/style.css">
	
<link rel="stylesheet" type="text/css" href="template/jquery-ui-1.9.2.custom.css">
<title>添加采购信息</title>

<script type="text/javascript" src="template/jquery.min.js">
</script><script type="text/javascript" src="template/jquery-ui-1.9.2.custom.min.js"></script>
<link rel="stylesheet" href="template/layer.css" id="layui_layer_skinlayercss">
<link href="template/WdatePicker.css" rel="stylesheet" type="text/css"></head>




<body>
	<article class="page-container">
		<form action="?action=edit" name="form" method="post" class="form form-horizontal" id="form" onSubmit="">

			<div class="row cl">
				<label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>合同数字编号：</label>
				<div class="formControls col-xs-8 col-sm-9">
					<!-- <input type="text" class="input-text" value="" placeholder="" id="contractnum" name="contractnum" style="width:350px"> -->
					<select name="numerical" id="numerical" class="input-text" style="width:350px" onchange="doRequest()">
					<option value="">---请选择合同数字编号---</option>
					<c:forEach items="${list }" var="bean">
					<option value="${bean.numerical }" ${bean.numerical==purchBean.numerical? "selected" : "" }>${bean.numerical }</option>
					</c:forEach>
					</select>
				</div>
			</div>

			<div class="row cl">
				<label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>备件名称：</label>
				<div class="formControls col-xs-8 col-sm-9">
					<!-- <input type="text" class="input-text" value="" placeholder="" id="spare" name="spare" style="width:350px"> -->
					
					<select name="sqare" id="sqare" class="input-text" style="width:350px" onchange="doSqareRequest()">
					<option value="">---请选择备件名称---</option>
					<c:forEach items="${csList }" var="bean">
					<option value="${bean.sqarename }" ${bean.sqarename==purchBean.sqarename? "selected" : "" }>${bean.sqarename }</option>
					</c:forEach>
					</select>
					<span id="aaa"></span>
				</div>
			</div>

			<div class="row cl">
				<label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>规格型号：</label>
				<div class="formControls col-xs-8 col-sm-9">
					<input type="text" class="input-text" value="${purchBean.modeltype }" placeholder="" id="model" name="model" style="width:350px">
				</div>
			</div>

			<div class="row cl">
				<label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>计量单位：</label>
				<div class="formControls col-xs-8 col-sm-9">
					<input type="text" class="input-text" value="${purchBean.unit }" placeholder="" id="prickle" name="prickle" style="width:350px">
				</div>
			</div>

			<div class="row cl">
				<label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>数量：</label>
				<div class="formControls col-xs-8 col-sm-9">
					<input type="text" class="input-text" value="${purchBean.totalcount }" placeholder="" id="amount" name="amount" style="width:350px">
				</div>
			</div>

			<div class="row cl">
				<label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>单价：</label>
				<div class="formControls col-xs-8 col-sm-9">
					<input type="text" class="input-text" value="${purchBean.price }" placeholder="" id="price" name="price" style="width:350px">
				</div>

			</div>

			<div class="row cl">
				<label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>总金额：</label>
				<div class="formControls col-xs-8 col-sm-9">
					<input type="text" class="input-text" value="${purchBean.totalmoney }" placeholder="" id="totalmoney" name="totalmoney" style="width:350px">
				</div>

			</div>


			<div class="row cl">
				<label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>供货商名称：</label>
				<div class="formControls col-xs-8 col-sm-9">
					<input type="text" class="input-text" value="${purchBean.supplier }" placeholder="" id="supplier" name="supplier" style="width:350px">
				</div>
			</div>
			
			<div class="row cl">
				<label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>采购时间：</label>
				<div class="formControls col-xs-8 col-sm-9">
					<input type="text" name="purdate" value="${purchBean.purchasedate }" style="width:150px;" onFocus="WdatePicker({maxDate:&#39;%y-%M-%d&#39;})" id="purdate" class="input-text Wdate">
				</div>
			</div>

			<div class="row cl">
				<div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-3">
				<input type="hidden" name="id" value="${purchBean.id }" />
					<input class="btn btn-primary radius" id="send" type="submit" onClick="test()" value="  提交  ">
					<input type="button" class="btn btn-danger radius" id="transmit" value="关闭并返回"/>
						
				</div>
			</div>
		</form>
	</article>

	<script>
	//合同编号 备件名称二级联动
	var xhr;
	function createXMLHTTP(){
		if(window.ActiveXObject){
			xhr = new ActiveXObject("Microsoft.XMLHTTP");
		}else{
			xhr = new XMLHttpRequest();
		}
	}
	function checkState(){
		if(xhr.status==200&&xhr.readyState==4){
			document.getElementById("sqare").innerHTML = xhr.responseText;
		}
	}
	function doRequest(){
		createXMLHTTP();
		xhr.onreadystatechange = checkState;
		var numerical = document.getElementById("numerical").value;
		xhr.open("get", "purch_sqare_ajax.jsp?numerical="+numerical,true);
		xhr.send(null);
	}
	
	
	var xhr1;
	function createXMLHTTP1(){
		if(window.ActiveXObject){
			xhr1 = new ActiveXObject("Microsoft.XMLHTTP");
		}else{
			xhr1 = new XMLHttpRequest();
		}
	}
	function checkSqareState(){
		if(xhr1.status==200&&xhr1.readyState==4){
			var data = xhr1.responseText;
			var n = data.split(",");
			//alert(n[2]);
			document.getElementById("model").value=n[0];
			document.getElementById("prickle").value=n[1];
			document.getElementById("amount").value=n[2];
			document.getElementById("price").value=n[3];
			document.getElementById("totalmoney").value=n[4];
			
		}
	}
	function doSqareRequest(){
		createXMLHTTP1();
		xhr1.onreadystatechange = checkSqareState;
		var numerical = document.getElementById("numerical").value;
		var sqarename = document.getElementById("sqare").value;
		xhr1.open("get", "sqarechange.jsp?numerical="+numerical+"&sqare="+sqarename,true);
		xhr1.send("sqare="+encodeURI(sqarename));
	}
	
	</script>

	<script type="text/javascript" src="template/jquery.min.js"></script>
	<script type="text/javascript" src="template/layer.js"></script>
	<script type="text/javascript" src="template/laypage.js"></script>
	<script type="text/javascript" src="template/jquery.icheck.min.js"></script>
	<script type="text/javascript" src="template/WdatePicker.js"></script>
	<script type="text/javascript" src="template/jquery.dataTables.min.js"></script>
	<script type="text/javascript" src="template/jquery.validate.min.js"></script>
	<script type="text/javascript" src="template/validate-methods.js"></script>
	<script type="text/javascript" src="template/messages_zh.min.js"></script>
	<script type="text/javascript" src="template/H-ui.js"></script>
	<a href="javascript:void(0)" class="Hui-iconfont toTop" title="返回顶部" alt="返回顶部" style="display:none"></a>
	<script type="text/javascript" src="template/H-ui.admin.js"></script>

	<script type="text/javascript" src="template/jquery-ui-1.9.2.custom.min.js"></script>

	<script type="text/javascript">
	$(function(){ 
			//文本框失去焦点后`
			$('form :input').blur(function() {
								var $parent = $(this).parent();
								$parent.find(".formtips").remove();
								//验证合同数字编号 
								if ($(this).is('#numerical')) {
									 if (this.value == ""|| this.value.length != 6|| isNaN(this.value) == true) {
										var errorMsg = '请输入6位的合同数字编号.';
										$parent.append('<span class="formtips onError" style="color:red">'+ errorMsg + '</span>');
									} else if(this.value.trim()=="" || this.value.replace(/(^s*)|(s*$)/g, "").length ==0){
										var errorMsg = '输入内容不能为空格';
										$parent.append('<span class="formtips onError" style="color:red">'+ errorMsg + '</span>');
									} else {
										//var okMsg = '输入正确.'; 
										$parent.append('<span class="formtips onSuccess" style="color:green"></span>');
									}
								}
								//验证备件 
								if ($(this).is('#sqare')) {
									if (this.value == "") {
										var errorMsg = '备件名称不能为空.';
										$parent.append('<span class="formtips onError" style="color:red">'+ errorMsg + '</span>');
									} else if(this.value.trim()==""){
										var errorMsg = '输入内容不能为空格';
										$parent.append('<span class="formtips onError" style="color:red">'+ errorMsg + '</span>');
									}
									else {
										//document.getElementById("aaa").innerHTML="<image src="h-ui-images/ok.png">";
										//var okMsg = "输入正确！" 
										//okMsg.innerHTML = <img src="images/hi.png"/>;
										//return true;
										$parent.append('<span class="formtips onSuccess" style="color:green"></span>');
									}
								}

								//验证总金额 
								if ($(this).is('#totalmoney')) {
									if (this.value == "") {
										var errorMsg = '总金额不能为空.';
										$parent
												.append('<span class="formtips onError" style="color:red">'
														+ errorMsg + '</span>');
									}else if(this.value.trim()==""){
									var errorMsg = '输入内容不能为空格';

										$parent.append('<span class="formtips onError" style="color:red">'
														+ errorMsg + '</span>');
									}else{
										$parent
												.append('<span class="formtips onSuccess" style="color:green"></span>');
									}
								}
								
								if ($(this).is('#model')) {
									if (this.value == "") {
										var errorMsg = '规格型号不能为空.';
										$parent
												.append('<span class="formtips onError" style="color:red">'
														+ errorMsg + '</span>');
									}else if(this.value.trim()==""){
									var errorMsg = '输入内容不能为空格';

										$parent.append('<span class="formtips onError" style="color:red">'
														+ errorMsg + '</span>');
									}else{
										$parent
												.append('<span class="formtips onSuccess" style="color:green"></span>');
									}
								}

								//验证计量单位 
								if ($(this).is('#prickle')) {
									if (this.value == "") {
										var errorMsg = '计量单位不能为空.';
										$parent
												.append('<span class="formtips onError" style="color:red">'
														+ errorMsg + '</span>');
									} else if(this.value.trim()==""){
										var errorMsg = '输入内容不能为空格';

										$parent.append('<span class="formtips onError" style="color:red">'
														+ errorMsg + '</span>');
									}
									else {
										//var okMsg = '输入正确.'; 
										$parent
												.append('<span class="formtips onSuccess" style="color:green"></span>');
									}
								}

								//验证数量
								if ($(this).is('#amount')) {
									if (this.value == "") {
										var errorMsg = '数量不能为空.';
										$parent
												.append('<span class="formtips onError" style="color:red">'
														+ errorMsg + '</span>');
									} else if (isNaN(this.value) == true) {
										var errorMsg = '数量必须为数字.';
										$parent
												.append('<span class="formtips onError" style="color:red">'
														+ errorMsg + '</span>');
									} else {
										//var okMsg = '输入正确.'; 
										$parent
												.append('<span class="formtips onSuccess" style="color:green"></span>');
									}
								}

								//验证单价 
								if ($(this).is('#price')) {
									if (this.value == "") {
										var errorMsg = '单价不能为空.';
										$parent
												.append('<span class="formtips onError" style="color:red">'
														+ errorMsg + '</span>');
									} else if (isNaN(this.value) == true) {
										var errorMsg = '单价必须为数字.';
										$parent
												.append('<span class="formtips onError" style="color:red">'
														+ errorMsg + '</span>');
									} else {
										//var okMsg = '输入正确.'; 
										$parent
												.append('<span class="formtips onSuccess" style="color:green"></span>');
									}
								}

								//验证合同总额 
								if ($(this).is('#contracttotal')) {
									if (this.value == "") {
										var errorMsg = '合同总额不能为空.';
										$parent
												.append('<span class="formtips onError" style="color:red">'
														+ errorMsg + '</span>');
									} else if (isNaN(this.value) == true) {
										var errorMsg = '合同总额必须为数字.';
										$parent
												.append('<span class="formtips onError" style="color:red">'
														+ errorMsg + '</span>');
									} else if(this.value.trim()==""){
										var errorMsg = '输入内容不能为空格';

										$parent.append('<span class="formtips onError" style="color:red">'
														+ errorMsg + '</span>');
									}
									else {
										//var okMsg = '输入正确.'; 
										$parent
												.append('<span class="formtips onSuccess" style="color:green"></span>');
									}
								}

								//验证供货商名称
								if ($(this).is('#supplier')) {
									if (this.value == "") {
										var errorMsg = '供货商名称不能为空.';
										$parent
												.append('<span class="formtips onError" style="color:red">'
														+ errorMsg + '</span>');
									} else if(this.value.trim()==""){
										var errorMsg = '输入内容不能为空格';

										$parent.append('<span class="formtips onError" style="color:red">'
														+ errorMsg + '</span>');
									}
									else {
										//var okMsg = '输入正确.'; 
										$parent
												.append('<span class="formtips onSuccess" style="color:green"></span>');
									}
								}
								
								if( $(this).is('#purdate') ){ 
               						if( this.value==""  ){ 
                     					var errorMsg = '日期不能为空.'; 
                    					$parent.append('<span class="formtips onError" style="color:red">'+errorMsg+'</span>'); 
               						}
               						else{ 
                     					$parent.append('<span class="formtips onSuccess" style="color:green"></span>'); 
               						}
           			 			} 
							})
					.keyup(function() {
						$(this).triggerHandler("blur");
						$('#model').unbind("keyup");
					})
					.focus(function() {
						$(this).triggerHandler("blur");
						$('#model').unbind("focus");
					}
					);//end blur

			//提交，最终验证。 
			$('#send').click(function() {
		  $("form :input.required").trigger('blur');
		  var numError=$("form .onError").length;
		  if(numError!=0){
		  	  return false;
		  }
		  else{
		  	return true;
		  }
		 
     });
		})
	</script>
	
	<script type="text/javascript">
$(function() {
			$("#dialog-confirm").dialog({
				autoOpen: false,
				title : '系统提示',
				height: 190,
				width: 390,
				modal: false,
				resizable: false,
				draggable: false,
				buttons: {
					"确定": function() {
						$( this ).dialog( "close" );
					}
				}
			});
	 });
function test(){

	//验证为空
	for(var i=0;i<document.form.elements.length;i++)
         {
          if(document.form.elements[i].value=="" || document.form.elements[i].value.trim()=="")
          {
           document.form.elements[i].focus();
           return false;
          }
         }
   var contractnum = $('#contractnum').val();
   var spare = $('#spare').val();
   var model = $('#model').val();
	$.ajax({
		type : "POST",//ajax请求数据的方式   默认为get  post   还有put  delete  分别对应增删改查
		url :"/EMIS/addpurchaseservlet3",//servlet的地址，web.xml中配置  项目名+servlet的地址
		dataType : "html",//请求的数据格式  json或者xml
		data :{//传输的数据，以键值对方式传输，会根据datatype的类型自动转成xml或者json数据格式传输
	    contractnum : contractnum,
		spare : spare,
		model : model
		 },
		success :function(data){//data里面存放的是后台处理后返回到前台的数据 eg：success 或者 error等。
		console.log(data);
		if(data == "合同明细内没有此信息"){
		$("#dialog-confirm .text").html(data);
		
		$("#dialog-confirm").dialog("open");
		
		 } else {
		 	$("#form").submit();
		 }
		}
	});

}
</script> 

 <script type="text/javascript">
    $('#transmit').on('click', function(){
        //刷新页面
        parent.window.location.href='purchaslist.jsp';
        //获取窗口索引
        var index = parent.layer.getFrameIndex(window.name);
        //关闭弹出层
        parent.layer.close(index);
    });
</script>


<div class="ui-dialog ui-widget ui-widget-content ui-corner-all ui-dialog-buttons" tabindex="-1" role="dialog" aria-labelledby="ui-id-1" style="display: none; outline: 0px; z-index: 1000;"><div class="ui-dialog-titlebar ui-widget-header ui-corner-all ui-helper-clearfix"><span id="ui-id-1" class="ui-dialog-title">系统提示</span><a href="http://localhost:8080/EMIS/purchase_add.jsp#" class="ui-dialog-titlebar-close ui-corner-all" role="button"><span class="ui-icon ui-icon-closethick">close</span></a></div><div id="dialog-confirm" style="" class="ui-dialog-content ui-widget-content">
		<p>
			<span class="ui-icon ui-icon-alert" style="float: left; margin: 0 7px 20px 0;"></span>
			<span class="text"></span></p>
	</div><div class="ui-dialog-buttonpane ui-widget-content ui-helper-clearfix"><div class="ui-dialog-buttonset"><button type="button" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" role="button" aria-disabled="false"><span class="ui-button-text">确定</span></button></div></div></div></body></html>
	
	
	
	
	
	
	
	
	